#!/usr/bin/python
# -*- coding: UTF-8 -*-
#cron：0 8,10,15 * * *
import json
import re
import requests
import time
import datetime
import base64
t = time.time()
timestamp = int(round(t * 1000))
import json
import time
import random
import requests
import base64
import os
import sys
zhanghaomima=os.environ.get('xiwotoken')
delay_seconds = random.random() * 10  

# 加载通知服务
def load_send():
    cur_path = os.path.abspath(os.path.dirname(__file__))
    sys.path.append(cur_path)
    if os.path.exists(cur_path + "/notify2.py"):
        try:
            from notify2 import send
            return send
        except Exception as e:
            print(f"加载通知服务失败：{e}")
            return None
    else:
        print("加载通知服务失败")
        return None

###获取token
def token():
    headers = {
     'Cookie':'x-auth-app=EasiNote5; x-auth-token=; acw_tc=1a0c595817308041547398024e00e398f9ff3f4cdf743e11908bdb62cff81d',
     'X-APM-TraceId':'6a7e4cf4c71645dfb92c037d12f17709',
     'Content-Type':'application/json; charset=utf-8',
     'Content-Length':'109'}
    #payload=base64.b64decode()
    payload=base64.b64decode(zhanghaomima)
    html = requests.request("POST", "https://edu.seewo.com/api/v1/auth/login", headers=headers, data=payload)
    a = json.loads(html.text)
    token = a["data"]["token"]
    print(f'获取最新token：{token}')
    return token
a = token()
##每日签到
def chaxun():
    headers = {
        'Cookie': f'x-auth-token={a}; x-auth-app=EasiNote5; x-auth-brand=; client_version=5.2.4.8298; client_build_version=108298; client_flags=tabs; acw_tc=1a0c57d717310419592552369e010287431d3cb1c78b1f45ad77f675fa3b95',
        'X-APM-TraceId': '20a05125e71b4511b3867ff9d0a91fc9',
        'x-auth-app': 'EasiNote5',
        'x-auth-brand': '',
        'x-auth-timestamp': '1731043604156',
        'x-auth-sign': 'e768cae5914b7bf94af8d62c5d2a0b640fbb0acbb9808c8e100ea890d4fafa37'}
    payload = None

    response0 = requests.request("GET", "https://edu.seewo.com/api/v1/user/level", headers=headers, data=payload)
    mingzi = response0.json()["userLevel"]["nickName"]
    kongjian = response0.json()["userLevel"]["cloudSize"]
    kongjian1 = kongjian / 1024 / 1024 / 1024
    xianzaijingyan = response0.json()["userLevel"]["experience"]
    jibie = response0.json()["userLevel"]["level"]
    #shengjihaixu = response0.json()["userLevel"]["needExperience"]
    msg = f'用户姓名：{mingzi}\n现在级别：{jibie}\n现在空间：{kongjian1} GB\n现在经验:{xianzaijingyan}'
    return msg
def qiandaochaxun():
    import requests

    headers = {
        'sec-ch-ua': '"Chromium";v="101"',
        'accept': '*/*',
        'x-requested-with': 'XMLHttpRequest',
        'sec-ch-ua-mobile': '?0',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 EasiNote5/5.2.4.8298 Safari/537.36',
        'sec-ch-ua-platform': '"Windows"',
        'accept-language': 'zh-CN,zh;q=0.9',
        'sec-fetch-site': 'same-origin',
        'sec-fetch-mode': 'cors',
        'sec-fetch-dest': 'empty',
        'referer': 'https://easinote.seewo.com/embedpc/mission?type=1',
        'accept-encoding': 'gzip, deflate, br',
        'cookie': f'x-auth-token={a}; x-token={a}; x-auth-app=EasiNote5; x-auth-brand=; phone=15779752720; app-versions=5.2.4.8298; x-custom-version=Standard; sw_sid=user_undefined; EGG_SESS=3qeRnwijbM8VmHvY72woD46e7vbayEbD7o0Wce6PfSSBoSnsAVaeFrfZQk5XjBI4OQejhVHNlgBaU5qh83DiNl-K41awlIEpwsEGcyL8TKvX_t4C3GHj37O_olqrT8lJPxsSXvoZv1z6PgbjZU_7v_P9qLaOFjWxxmWj9iF_l98=; Hm_lvt_4ef81bf49656fa38a45b802406a5f13c=1730804267; Hm_lpvt_4ef81bf49656fa38a45b802406a5f13c=1730804267; HMACCOUNT=F4C64553A9D562AC; acw_tc=ac11000117308065611647345e00a3e159b7a028819a34f374c800f4fddc35'}
    payload = None

    response0 = requests.request("GET",
                                 "https://easinote.seewo.com/embedpc/com/apis?api=MISSION_TODAY_RECORD&_=1730806525025",
                                 headers=headers, data=payload)
    time.sleep(delay_seconds)
    jiangli1 = response0.json()["data"]["lotteryRecord"]["prizeName"]
    jiangli2 = response0.json()["data"]["signRecord"]["prizeName"]
    qiandaotianshu =  response0.json()["data"]["signRecord"]["currentDay"]
    msg=f'今天已经签到过了，已签到{qiandaotianshu}天，\n签到获得：{jiangli1}和{jiangli2}'
    return msg
def qiandao():
    headers = {
        'sec-ch-ua': '"Chromium";v="101"',
        'accept': '*/*',
        'x-requested-with': 'XMLHttpRequest',
        'sec-ch-ua-mobile': '?0',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 EasiNote5/5.2.4.8298 Safari/537.36',
        'sec-ch-ua-platform': '"Windows"',
        'accept-language': 'zh-CN,zh;q=0.9',
        'sec-fetch-site': 'same-origin',
        'sec-fetch-mode': 'cors',
        'sec-fetch-dest': 'empty',
        'referer': 'https://easinote.seewo.com/embedpc/mission?type=1',
        'accept-encoding': 'gzip, deflate, br',
        'cookie': f'x-auth-token={a}; x-token={a}; x-auth-app=EasiNote5; x-auth-brand=; phone=15779752720; app-versions=5.2.4.8298; x-custom-version=Standard; acw_tc=1a0c579817308043018994766e0049f651f779475fdc0737e5437ebceb8694; sw_sid=user_undefined; EGG_SESS=3qeRnwijbM8VmHvY72woD46e7vbayEbD7o0Wce6PfSSBoSnsAVaeFrfZQk5XjBI4OQejhVHNlgBaU5qh83DiNl-K41awlIEpwsEGcyL8TKvX_t4C3GHj37O_olqrT8lJPxsSXvoZv1z6PgbjZU_7v_P9qLaOFjWxxmWj9iF_l98=; Hm_lvt_4ef81bf49656fa38a45b802406a5f13c=1730804267; Hm_lpvt_4ef81bf49656fa38a45b802406a5f13c=1730804267; HMACCOUNT=F4C64553A9D562AC'}
    payload = None
    response0 = requests.request("GET",f"https://easinote.seewo.com/embedpc/com/apis?api=MISSION_SIGN_LOTTERY&_={timestamp}",headers=headers, data=payload)
    #result = response0.json()["data"]["lotteryRecord"]["continuous_day"]
    result = response0.json()["message"]
    print(result)
    if result == 'ok':
        jiangli1 = response0.json()["data"]["lotteryRecord"]["prizeName"]
        jiangli2 = response0.json()["data"]["signRecord"]["prizeName"]
        qiandaotianshu =  response0.json()["data"]["signRecord"]["currentDay"]
        msg = f'签到成功，已签到{qiandaotianshu}天，\n签到获得：{jiangli1}和{jiangli2}'
        print(msg)
    elif result == 'sign lottery fail':
        msg = qiandaochaxun()
        print(qiandaochaxun())
    else:
        msg = '登录失败了吧？快检查一下原因'
        print(msg)
    return msg
##任务开始
def taskbegin():

    headers = {
        'sec-ch-ua': '"Chromium";v="101"',
        'accept': '*/*',
        'x-requested-with': 'XMLHttpRequest',
        'sec-ch-ua-mobile': '?0',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 EasiNote5/5.2.4.8298 Safari/537.36',
        'sec-ch-ua-platform': '"Windows"',
        'accept-language': 'zh-CN,zh;q=0.9',
        'sec-fetch-site': 'same-origin',
        'sec-fetch-mode': 'cors',
        'sec-fetch-dest': 'empty',
        'referer': 'https://easinote.seewo.com/embedpc/mission?type=1',
        'accept-encoding': 'gzip, deflate, br',
        'cookie': f'x-auth-token={a}; x-token={a}; x-auth-app=EasiNote5; x-auth-brand=; phone=15779752720; app-versions=5.2.4.8298; x-custom-version=Standard; acw_tc=1a0c579817308043018994766e0049f651f779475fdc0737e5437ebceb8694; sw_sid=user_undefined; EGG_SESS=3qeRnwijbM8VmHvY72woD46e7vbayEbD7o0Wce6PfSSBoSnsAVaeFrfZQk5XjBI4OQejhVHNlgBaU5qh83DiNl-K41awlIEpwsEGcyL8TKvX_t4C3GHj37O_olqrT8lJPxsSXvoZv1z6PgbjZU_7v_P9qLaOFjWxxmWj9iF_l98=; Hm_lvt_4ef81bf49656fa38a45b802406a5f13c=1730804267; Hm_lpvt_4ef81bf49656fa38a45b802406a5f13c=1730804267; HMACCOUNT=F4C64553A9D562AC'}

    payload = None
    response0 = requests.request("GET", "https://easinote.seewo.com/embedpc/com/apis?api=MISSION_TASK&_=1730804266508",headers=headers, data=payload)
    #print(response0.text)
    result = response0.json()["message"]
    if result == 'ok':
        msg = '今天的希沃任务已经开始了！'
    else:
        msg = '登录失败了吧？快检查一下原因'
    return msg
##开始做任务了
#使用时长：
def zuorenwu():

    headers = {
        'User-Agent': 'Fiddler-Everywhere',
        'Content-Type': 'application/json',
        'Content-Length': '25',
        'Cookie': f'x-auth-token={a}; x-auth-app=EasiNote5; x-auth-brand=; client_version=5.2.4.8298; client_build_version=108298; client_flags=tabs; acw_tc=1a0c579a17308060776735972e00746bb2fa3fa65f8eacc5480f74fb54d982',
        'X-APM-TraceId': '002c1a9b993c4c30874f771c57a5c0d4',
        'x-auth-app': 'EasiNote5',
        'x-auth-brand': '',
        'x-auth-timestamp': '1730806042507',
        'x-auth-sign': '56fb83e79be2c094617386271948b00765a8cb8fa685c24b65c581d69214fa5c',
        'Content-Type': 'application/json; charset=utf-8'}
    payload = base64.b64decode("eyJ0YXNrSWQiOjMsInBhcmFtIjoiMzEifQ==")

    #response1 = requests.request("POST", "https://edu.seewo.com/api/v2/task/log", headers=headers, data=payload)
    #response2 = requests.request("POST", "https://edu.seewo.com/api/v2/task/log", headers=headers, data=payload)
    headers1 = {
        'User-Agent': 'Fiddler-Everywhere',
        'Content-Length': '23',
        'Cookie': f'x-auth-token={a}; x-auth-app=EasiNote5; x-auth-brand=; client_version=5.2.4.8298; client_build_version=108298; client_flags=tabs; acw_tc=1a0c579a17308060776735972e00746bb2fa3fa65f8eacc5480f74fb54d982',
        'X-APM-TraceId': '48ff827e08044a4ebf95b822460e1052',
        'x-auth-app': 'EasiNote5',
        'x-auth-brand': '',
        'x-auth-timestamp': '1730806052309',
        'x-auth-sign': '1241620f674cb479c3a4cbcaea73d82ea818611712c0f02b1588fb7a85c313d4',
        'Content-Type': 'application/json; charset=utf-8'}
    headers4 =  {
            'Cookie': f'x-auth-token={a}; x-auth-app=EasiNote5; x-auth-brand=; client_version=5.2.4.8298; client_build_version=108298; client_flags=tabs; acw_tc=1a0c57d817309915802155000e01046c3e3f53cc02f1bef502e5b1002519a3',
            'X-APM-TraceId': '8d8c4d46ab51490faf8703a3cfd3efd3',
            'x-auth-app': 'EasiNote5',
            'x-auth-brand': '',
            'x-auth-timestamp': '1730991604217',
            'x-auth-sign': 'd9acbc514ee8c37e18787f4c35f9e39fb006105164aa195278aa4c03ac257fbd',
            'Content-Type': 'application/json; charset=utf-8',
            'Content-Length': '23'}
    headers5 = {
        'Cookie': f'x-auth-token={a}; x-auth-app=EasiNote5; x-auth-brand=; client_version=5.2.4.8298; client_build_version=108298; client_flags=tabs; acw_tc=1a0c579a17308060776735972e00746bb2fa3fa65f8eacc5480f74fb54d982',
        'X-APM-TraceId': 'c39cfa437ce84f0ab56cf703523b2c13',
        'x-auth-app': 'EasiNote5',
        'x-auth-brand': '',
        'x-auth-timestamp': '1730806356642',
        'x-auth-sign': 'ecae5cb7691da202636113b3da128ef51c244d9a244781bc73e7c6bef0cae572',
        'Content-Type': 'application/json; charset=utf-8',
        'Content-Length': '23'}
    payload1=base64.b64decode("eyJ0YXNrSWQiOjIsInBhcmFtIjoiIn0=")
    payload2=base64.b64decode("eyJ0YXNrSWQiOjQsInBhcmFtIjoiIn0=")
    payload3=base64.b64decode("eyJ0YXNrSWQiOjUsInBhcmFtIjoiIn0=")
    for i in range(5):
        time.sleep(delay_seconds)
        response0 = requests.request("POST", "https://edu.seewo.com/api/v2/task/log", headers=headers, data=payload)
        result = response0.json()["message"]
        if result=='task not begin':
            taskbegin()
        time.sleep(delay_seconds)
        response3 = requests.request("POST", "https://edu.seewo.com/api/v2/task/log", headers=headers1, data=payload1)
        time.sleep(delay_seconds)
        response7 = requests.request("POST", "https://edu.seewo.com/api/v2/task/log", headers=headers5, data=payload3)
        time.sleep(delay_seconds)
        response6 = requests.request("POST", "https://edu.seewo.com/api/v2/task/log", headers=headers4, data=payload2)


        print(response0.text)
        print(response3.text)
        print(response7.text)
        print(response6.text)
##领取任务奖励
def lingqujiangli():
    headers1 = {
    #PUT https://easinote.seewo.com/embedpc/com/apis?api=MISSION_TASK_FINISH HTTP/1.1
    'Host': 'easinote.seewo.com',
    'Connection': 'keep-alive',
    'Content-Length': '9',
    'sec-ch-ua': '"Chromium";v="91"',
    'Accept': '*/*',
    'X-Requested-With': 'XMLHttpRequest',
    'sec-ch-ua-mobile': '?0',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
    'Accept-Language': '',
    'Origin': 'https://easinote.seewo.com',
    'Sec-Fetch-Site': 'same-origin',
    'Sec-Fetch-Mode': 'cors',
    'Sec-Fetch-Dest': 'empty',
    'Referer': 'https://easinote.seewo.com/embedpc/mission?type=1',
    'Accept-Encoding': 'gzip, deflate, br',
    'Cookie': f'Hm_lvt_67f101c3c1685f877ac534ff2517d656=1631935311; sw_sid=user_undefined; x-auth-token={a}; x-token={a}; x-auth-app=EasiNote5; x-auth-brand=; phone=15779752720; app-versions=5.2.0.6718; x-custom-version=Standard; x-special-version=; EGG_SESS=3qeRnwijbM8VmHvY72woD73u8dVJCiGv4iQsyDaU0VrkyyjG8x7ruBf7bs-derL1BKcUKDahUJePY63hUiXXFLyQ7JQm1NRntmV6dT09Y-ax_Yf1vd6sh4g9DzBu_odxrXegZp_AzIyF0jrRiFnnbyb6C88l-Yqite-HLbk6q_Y=; Hm_lvt_4ef81bf49656fa38a45b802406a5f13c=1638888510,1638961281,1639568415; ab_uid=5f9522cc9d0d88fde1d40b459bb1feb3; Hm_lpvt_4ef81bf49656fa38a45b802406a5f13c=1639572104',

    #taskId=2
    }
    renwuid = [1,2,3,4,5,99]
    for id in renwuid:
        print(id)
        if id ==99:
            print("先执行这个")
            taskbegin()
        url2 = 'https://easinote.seewo.com/embedpc/com/apis?api=MISSION_TASK_FINISH'
        params1 = {'taskId':id}
        res1 = requests.put(url= url2,data=params1,headers=headers1)
        print(res1.text)
        '''
        tui1 = res1.text
        tuilist.append(tui1)
        print(res1.text)
    list1= ''.join(tuilist)
    print(list1)
    '''
def chaxunrenwu():
    zuorenwu()
    lingqujiangli()
    def extract_name_and_currentTip(json_data):
        result_dict = {}

        if isinstance(json_data, dict):
            currentTip = json_data.get("currentTip")
            name = json_data.get("name")
            if currentTip is not None:
                result_dict[name] = currentTip
            for key, value in json_data.items():
                result_dict.update(extract_name_and_currentTip(value))
        elif isinstance(json_data, list):
            for item in json_data:
                result_dict.update(extract_name_and_currentTip(item))

        return result_dict


    # 发起请求，这里假设请求的URL是 'https://example.com/api/your_endpoint'，你需要替换成实际的URL
    headers = {
        'sec-ch-ua': '"Chromium";v="101"',
        'accept': '*/*',
        'x-requested-with': 'XMLHttpRequest',
        'sec-ch-ua-mobile': '?0',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.4951.67 EasiNote5/5.2.4.8298 Safari/537.36',
        'sec-ch-ua-platform': '"Windows"',
        'accept-language': 'zh-CN,zh;q=0.9',
        'sec-fetch-site': 'same-origin',
        'sec-fetch-mode': 'cors',
        'sec-fetch-dest': 'empty',
        'referer': 'https://easinote.seewo.com/embedpc/mission?type=1',
        'accept-encoding': 'gzip, deflate, br',
        'cookie': f'x-auth-token={a}; x-token={a}; x-auth-app=EasiNote5; x-auth-brand=; phone=15779752720; app-versions=5.2.4.8298; x-custom-version=Standard; acw_tc=ac11000117310361574458097e003bc084b9394f185f854116d8cb596d93dc; sw_sid=user_undefined; EGG_SESS=3qeRnwijbM8VmHvY72woD05HZ3k10AEk-cGnadWjmyQX7oZyTzM7J3YliqDX1kQsoGKREAvYFQ9Vq0NvkMs58AvGokV7IFCgjgeUiSB9cnkuwynI_igTT6cSJOar9GWezBCXH0E74BVbzpHx8E6iBgDTdNuRIRIeOUKlcjbETeo=; Hm_lvt_4ef81bf49656fa38a45b802406a5f13c=1730804267,1730991582,1731036121; Hm_lpvt_4ef81bf49656fa38a45b802406a5f13c=1731036121; HMACCOUNT=B65A7F6BED828215'}
    payload = None

    response = requests.request("GET", "https://easinote.seewo.com/embedpc/com/apis?api=MISSION_TASK&_=1731036120324",
                                 headers=headers, data=payload)

    # 检查请求是否成功
    if response.status_code == 200:
        # 获取响应中的JSON数据
        json_data_obj = response.json()

        # 调用函数提取满足条件的name值和对应的currentTip值
        result = extract_name_and_currentTip(json_data_obj)

        # 将结果转化为字符串并换行显示
        result_str = ""
        for name, currentTip in result.items():
            result_str += f"{name}: {currentTip}\n"


    else:
        print(f"请求失败，状态码: {image_url}")
    return result_str
#msg = f'{qiandao()}\n{chaxunrenwu()}'
#print(msg)

# 将编码结果字典转换为JSON字符串
#data = json.dumps(encoded_results)

#print(data)
title = "希沃任务通知"
b=f'--------🌸用户信息🌸-------'
c=f'--------🌸签到任务🌸-------'
d=f'--------🌸其它任务🌸-------'
e=f'--------🌸任务完成🌸-------'
contents =  f'{b}\n{chaxun()}\n\n{c}\n{qiandao()}\n\n{d}\n{chaxunrenwu()}\n\n{e}\n{chaxun()}'
print(contents)
send_notify = load_send()
send_notify(title, contents)
# 将编码结果字典转换为JSON字符串
#data = json.dumps(encoded_results)

#print(data)